


#include <DrawUtils.h>

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>


/* static */ void DrawUtils::drawQuad(const Vec4& v1, const Vec4& v2, const Vec4& v3, const Vec4& v4, const Vec4& n)
{
	glBegin(GL_QUADS);
	
	glNormal3fv(&n[0]);
	glVertex3fv(&v1[0]);
	glVertex3fv(&v2[0]);
	glVertex3fv(&v3[0]);
	glVertex3fv(&v4[0]);
	glEnd();
}


/* static */ void DrawUtils::drawBox(Vec4 halfExtents)
{
	Vec4 v[8];
	unsigned int i,j;
	
	for (i = 0; i < 8; ++i) 
		for (j = 0; j < 3; ++j) 
			v[i][j] = halfExtents[j] * ( -1.0f + 2.0f*(float) ( (i >> j) & 1 ) );
	
	/*
	 
	 y        
	 |        
	 2----3     6----7
	 | -z |     | +z |
	 0----1-x   4----5
	 
	 */
	
	const Vec4 x_pos = Vec4(1, 0, 0);
	const Vec4 x_neg = Vec4(-1, 0, 0);
	
	const Vec4 y_pos = Vec4(0, 1, 0);
	const Vec4 y_neg = Vec4(0,-1, 0);
	
	const Vec4 z_pos = Vec4(0, 0, 1);
	const Vec4 z_neg = Vec4(0, 0,-1);
	
	drawQuad(v[0],v[2],v[3],v[1], z_neg);
	drawQuad(v[5],v[7],v[6],v[4], z_pos);
	drawQuad(v[0],v[1],v[5],v[4], y_neg);
	drawQuad(v[2],v[6],v[7],v[3], y_pos);
	drawQuad(v[0],v[4],v[6],v[2], x_neg);
	drawQuad(v[3],v[7],v[5],v[1], x_pos);
}

/* static */ void DrawUtils::drawCrosshairs(float size, const Vec4& P)
{
	Vec4 P0(P); Vec4 P1(P); 
	Vec4 P2(P); Vec4 P3(P); 
	Vec4 P4(P); Vec4 P5(P);

	P0[0] += size; P1[0] -= size; 
	P2[1] += size; P3[1] -= size;
	P4[2] += size; P5[2] -= size;
	
	glBegin(GL_LINES);
	
	glVertex3fv(&P0[0]);
	glVertex3fv(&P1[0]);
	
	glVertex3fv(&P2[0]);
	glVertex3fv(&P3[0]);
	
	glVertex3fv(&P4[0]);
	glVertex3fv(&P5[0]);

	glEnd();
}




